package com.stu.reflection.util;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class MethodUtil{
	private Class<?> clazz ;
	public MethodUtil(String className) {
		try {
			this.clazz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取指定方法的简单信息显示
	 * @param methodName 方法名
	 * @param params 方法参数
	 * @return 
	 */
	public String getSimpleMethodName(String methodName,Class<?>...params) {
		Method method = null;
		try {
			method = this.clazz.getDeclaredMethod(methodName, params);
		} catch (NoSuchMethodException | SecurityException e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer();
		sb.append(Modifier.toString(method.getModifiers())+" ");
		sb.append(method.getReturnType().getSimpleName()+" ");
		sb.append(method.getName()+"(");
		Class<?>[] types = method.getParameterTypes();
		for(int i=0;i<types.length;i++) {
			sb.append(types[i].getSimpleName()+" arg"+i);
			if(i<types.length-1) sb.append(",");
		}
		sb.append(")");
		Class<?>[] exceptionTypes = method.getExceptionTypes();
		int length = exceptionTypes.length;
		if(length>0) {
			sb.append(" throws ");
			for(int i=0;i<length;i++) {
				sb.append(exceptionTypes[i].getSimpleName());
				if(i<length-1) sb.append(",");
			}
		}
		return sb.toString();
	}

  public static void main(String[] args) {
    System.out.println(new MethodUtil("java.lang.String").getSimpleMethodName("equals",Object.class));
  }
}